package com.zhq.apputil.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;


import com.zhq.apputil.R;
import com.zhq.apputil.utils.ScreenUtils;

import androidx.annotation.Nullable;

/****
 * 带有标题的titleBar
 */
public class TitleBarLayout extends LinearLayout {
    private static final int TITLE_BAR_BACK = 0;
    private static final int TITLE_BAR_TITLE = 1;
    private static final int TITLE_BAR_RIGHT = 2;
    public static final int LEFT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3;
    private int imageSrc;
    private int mHeight, mWidth;
    private int leftLocation, leftPaddingSize;//左边按钮字体间距
    private int rightLocation, rightPaddingSize;//右边按钮字体间距
    private int textTitleColor, backgroundColor, titleLeftColor, titleRightColor, titleBarFontSize, titleFontSize;
    private String titleName, titleRightName, titleLeftName;
    private Drawable backgroundResource;

    public TitleBarLayout(Context context) {
        super(context);
        initLayout(context);
    }

    public TitleBarLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }


    @SuppressLint({"ResourceAsColor", "ResourceType"})
    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView);
        textTitleColor = typedArray.getColor(R.styleable.TitleBarView_title_color, Color.parseColor("#000000"));
        backgroundColor = typedArray.getColor(R.styleable.TitleBarView_titleBar_background_color, Color.parseColor("#FFFFFF"));
        titleLeftColor = typedArray.getColor(R.styleable.TitleBarView_titleLeft_font_color, Color.parseColor("#000000"));
        titleRightColor = typedArray.getColor(R.styleable.TitleBarView_titleRight_font_color, Color.parseColor("#000000"));
        titleBarFontSize = typedArray.getDimensionPixelSize(R.styleable.TitleBarView_titleBar_font_size, (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 0, getResources().getDisplayMetrics()));
        titleFontSize = typedArray.getDimensionPixelSize(R.styleable.TitleBarView_titleRight_font_size, (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 0, getResources().getDisplayMetrics()));
        titleName = typedArray.getString(R.styleable.TitleBarView_title_name);
        titleRightName = typedArray.getString(R.styleable.TitleBarView_titleRight_name);
        titleLeftName = typedArray.getString(R.styleable.TitleBarView_titleLeft_name);
        mWidth = typedArray.getDimensionPixelSize(R.styleable.TitleBarView_titleLeft_imageWidth, (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 0, getResources().getDisplayMetrics()));
        mHeight = typedArray.getDimensionPixelSize(R.styleable.TitleBarView_titleLeft_imageHeight, (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 0, getResources().getDisplayMetrics()));
        imageSrc = typedArray.getResourceId(R.styleable.TitleBarView_titleLeft_drawable_src, 0);
        typedArray.recycle();
        initLayout(context);
    }

    /****
     * 初始化布局
     * @param context
     */
    public void initLayout(Context context) {
        setOrientation(LinearLayout.HORIZONTAL);//设置位置
        setBackgroundColor(backgroundColor);
        for (int i = 0; i < 3; i++) {
            IconTextView iconTextView = new IconTextView(context);
            iconTextView.setTag(i);
            switch (i) {
                case TITLE_BAR_BACK:
                    iconTextView.setTextColor(titleLeftColor);
                    iconTextView.setText(titleLeftName);
                    iconTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, titleFontSize);
                    iconTextView.setGravity(Gravity.CENTER);
                    if (imageSrc != 0) {
                        iconTextView.setIconDrawable(imageSrc, mWidth, IconTextView.LEFT);
                    }
                    setFontPaddingLocation(iconTextView, leftLocation, leftPaddingSize);
                    iconTextView.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f));
                    break;
                case TITLE_BAR_TITLE:
                    iconTextView.setGravity(Gravity.CENTER);
                    iconTextView.setTextColor(textTitleColor);
                    iconTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, titleBarFontSize);
                    iconTextView.setText(convertString(titleName));
                    iconTextView.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 2.0f));
                    break;
                case TITLE_BAR_RIGHT:
                    iconTextView.setGravity(Gravity.CENTER | Gravity.RIGHT);
                    iconTextView.setTextColor(titleRightColor);
                    iconTextView.setText(convertString(titleRightName));
                    iconTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, titleFontSize);
                    setFontPaddingLocation(iconTextView, rightLocation, rightPaddingSize);
                    iconTextView.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f));
                    break;
            }
            addView(iconTextView);
        }
        LayoutParams lp = new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.dpToPx(getResources(), 50));
        setLayoutParams(lp);
    }


    //设置字体的间距
    private void setFontPaddingLocation(IconTextView textView, int location, int paddingSize) {
        int padding = ScreenUtils.dpToPx(getResources(), paddingSize);
        switch (location) {
            case LEFT:
                textView.setPadding(padding, 0, 0, 0);
                break;
            case TOP:
                textView.setPadding(0, padding, 0, 0);
                break;
            case RIGHT:
                textView.setPadding(0, 0, padding, 0);
                break;
            case BOTTOM:
                textView.setPadding(0, 0, 0, padding);
                break;
        }
    }


    /****
     * 设置TitleBar背景颜色
     * @param color
     * @return
     */
    public TitleBarLayout setTitleBarBackgroundColor(int color) {
        this.backgroundColor = getResources().getColor(color);
        return this;
    }

    /**
     * 设置TitleBar背景图片
     *
     * @param backgroundResource
     * @return
     */
    public TitleBarLayout setTitleBarBackground(Drawable backgroundResource) {
        this.backgroundResource = backgroundResource;
        return this;
    }


    /****
     * 设置TitleBar返回键字体颜色
     * @param color
     * @return
     */
    public TitleBarLayout setTitleBarBackFontColor(int color) {
        this.titleLeftColor = getResources().getColor(color);
        return this;
    }

    /****
     * 设置TitleBar右边控件字体颜色
     * @param color
     * @return
     */
    public TitleBarLayout setTitleRightFontColor(int color) {
        this.titleRightColor = getResources().getColor(color);
        return this;
    }

    /***
     * 设置TitleBar标题名称
     * @param name
     * @return
     */
    public TitleBarLayout setTitleBarTitleName(String name) {
        this.titleName = name;
        return this;
    }

    /***
     * 设置TitleBar返回键名称
     * @param name
     * @return
     */
    public TitleBarLayout setTitleBarBackName(String name) {
        this.titleLeftName = name;
        return this;
    }

    /***
     * 设置TitleBar头部颜色
     * @param color
     * @return
     */
    public TitleBarLayout setTitleBarTitleColor(int color) {
        this.textTitleColor = getResources().getColor(color);
        return this;
    }

    /***
     * 设置TitleBar右边控件名称
     * @param name
     * @return
     */
    public TitleBarLayout setTitleBarRightName(String name) {
        this.titleRightName = name;
        return this;
    }

    /***
     * 设置TitleBar左右控件字体大小
     * @param fontSize
     * @return
     */
    public TitleBarLayout setTitleBarFontSize(int fontSize) {
        this.titleFontSize = fontSize;
        return this;
    }

    /***
     * 设置TitleBar标题字体大小
     * @param titleFontSize
     * @return
     */
    public TitleBarLayout setTitleBarTitleFontSize(int titleFontSize) {
        this.titleBarFontSize = titleFontSize;
        return this;
    }

    /**
     * @param location:设置左边按钮字体的位置
     * @param paddingSize：设置左边字体的位置偏移距离
     **/
    public TitleBarLayout setFontLeftPadding(int location, int paddingSize) {
        this.leftLocation = location;
        this.leftPaddingSize = paddingSize;
        return this;
    }

    /**
     * @param location:设置右边字体的位置
     * @param paddingSize：设置右边字体的位置偏移距离
     **/
    public TitleBarLayout setFontRightPadding(int location, int paddingSize) {
        this.rightLocation = location;
        this.rightPaddingSize = paddingSize;
        return this;
    }

    /****
     * 设置左边图片大小
     * @param mWidth
     * @param mHeight
     * @return
     */
    public TitleBarLayout setBackDrawableLeftWidthHeight(int mWidth, int mHeight) {
        this.mWidth = mWidth;
        this.mHeight = mHeight;
        return this;
    }


    /***
     * 设置TitleBar返回键图片路径
     * @param imageSrc
     * @return
     */
    public TitleBarLayout setTitleBarBackDrawableLeftSrc(int imageSrc) {
        this.imageSrc = imageSrc;
        return this;
    }

    /***
     * 是否显示右边控件显示或隐藏
     * @param visible
     * @return
     */
    public TitleBarLayout setTitleBarRightVisible(int visible) {
        ((IconTextView) getChildAt(TITLE_BAR_RIGHT)).setVisibility(visible);
        return this;
    }

    /***
     * 为左边点击事件
     * @param onClickListener
     * @return
     */
    public TitleBarLayout setTitleBarBackOnClickListener(OnClickListener onClickListener) {
        ((IconTextView) getChildAt(TITLE_BAR_BACK)).setOnClickListener(onClickListener);
        return this;
    }

    /***
     * 为右边点击事件
     * @param onClickListener
     * @return
     */
    public TitleBarLayout setTitleBarRightOnClickListener(OnClickListener onClickListener) {
        ((IconTextView) getChildAt(TITLE_BAR_RIGHT)).setOnClickListener(onClickListener);
        return this;
    }

    /***
     * 获取标题
     * @return string
     */
    public String getTitleName() {
        return ((IconTextView) getChildAt(TITLE_BAR_TITLE)).getText().toString().trim();
    }

    public void build() {
        int length = getChildCount();
        for (int i = 0; i < length; i++) {
            IconTextView iconTextView = (IconTextView) getChildAt(i);
            switch (i) {
                case 0:
                    iconTextView.setTextColor(titleLeftColor);
                    iconTextView.setText(titleLeftName);
                    iconTextView.setTextSize(titleFontSize);
                    if (imageSrc != 0 && mWidth > 0) {
                        iconTextView.setIconDrawable(imageSrc, mWidth, IconTextView.LEFT);
                    }
                    setFontPaddingLocation(iconTextView, leftLocation, leftPaddingSize);
                    break;
                case 1:
                    iconTextView.setTextColor(textTitleColor);
                    iconTextView.setTextSize(titleBarFontSize);
                    iconTextView.setText(convertString(titleName));
                    break;
                case 2:
                    iconTextView.setTextColor(titleRightColor);
                    iconTextView.setText(convertString(titleRightName));
                    iconTextView.setTextSize(titleFontSize);
                    setFontPaddingLocation(iconTextView, rightLocation, rightPaddingSize);
                    break;
            }
        }
        if (backgroundResource != null) {
            setBackground(backgroundResource);
        } else {
            setBackgroundColor(backgroundColor);
        }
    }


    /****
     * 判断是否为空
     * @param result
     * @return
     */
    public String convertString(String result) {
        return TextUtils.isEmpty(result) ? "" : result;
    }

}
